/**
 * 获取支付后的跳转链接
 * @param {Object} baseInfo 基本信息
 * @param {Object} goodsInfo 商品信息 
 * @param {Object} marketingData 营销信息
 * @param {String} shareUserId 分享人id
 * @param {String} orderId 订单id
 * @param {Boolean} isSplit 是否拆单 
 * @returns Object {paySuccessUrl支付成功后跳转的链接，payFailUrl支付失败后跳转的链接}
 */
export function getJumpUrls(
  baseInfo,
  goodsInfo,
  marketingData,
  shareUserId,
  orderId,
  isSplit,
  teamBuyTaskId
) {
  const paySuccessUrl = getPaySuccessUrl(
    baseInfo,
    marketingData,
    goodsInfo,
    shareUserId,
    orderId,
    isSplit,
    teamBuyTaskId
  );
  //paypal下单需要传支付和待支付页面url，后端拼orderId给PayPal方，所以这里判断orderId是否存在，不存在就不传
  const payFailUrl = isSplit
    ? `/v1/usercenter/order/order_list?order_state=1`
    : orderId ? `/v1/usercenter/order/order_details?order_id=${orderId}`:`/v1/usercenter/order/order_details`;
  return {
    paySuccessUrl,
    payFailUrl,
  };
}

function getTeambuyPaySuccessUrl(
  baseInfo,
  marketingData,
  goodsInfo,
  shareUserId,
  teamBuyTaskId
) {
  if (baseInfo.isCart) return "";

  const {  taskId } = baseInfo;
  const { activity_type, team_buy_id } = marketingData || {};

  let paySuccessUrl = "";
  if (activity_type == 6) {
    const { resource_type, resource_id } = goodsInfo;
    const teamtaskId =  taskId || teamBuyTaskId || '';
    const queryObj = {
      team_buy_id,
      task_id: teamtaskId,
      resource_id,
      product_id: resource_id,
      resource_type,
      share_type: 5,
      share_user_id: shareUserId,
    };
    paySuccessUrl = `/teambuy/details?${joinParams(queryObj)}`;
  }
  return paySuccessUrl;
}

export function getPaySuccessUrl(
  baseInfo,
  marketingData,
  goodsInfo,
  shareUserId,
  orderId,
  isSplit, //是否拆单
  teamBuyTaskId
) {
  const {
    isFissionPoster,
    activityId,
    isTeam,
    aliveId,
    aliveBackUrl,
    content_app_id,
    isIntegral,
    isCart,
    associatedCircleId,
    is_ai_community,
    origin_community_id
  } = baseInfo;

  //裂变海报
  if (isFissionPoster) {
    return `/p/t/v1/ecommerce/marketing/fission_poster/page_load?page_type=0&activity_id=${activityId}`;
  }

  //拼团
  if (isTeam) {
    return getTeambuyPaySuccessUrl(
      baseInfo,
      marketingData,
      goodsInfo,
      shareUserId,
      teamBuyTaskId
    );
  }

  const queryObj = {
    order_id: orderId,
    skuId: goodsInfo.sku_id,
    isSplit: isSplit || '',
    integralRedeem: isIntegral ? 1 : null,
    content_app_id,
    cart:isCart ? 1 : '',
    associatedCircleId,
    is_ai_community,
    origin_community_id
  };

  const url = `/v1/goods/pay_complete?${joinParams(queryObj)}`;

  //直播
  if (aliveBackUrl) {
    if (aliveId) {
      return `${url}&isFromAlive=true&aliveBackUrl=${encodeURIComponent(
        decodeURIComponent(aliveBackUrl)
      )}`;
    } else {
      return sessionStorage.getItem("aliveBackUrl") || aliveBackUrl;
    }
  }

  return url;
}

function joinParams(queryObj) {
  return Object.keys(queryObj || {})
    .filter((key) => {
      const value = queryObj[key];
      return typeof value === "number" || value;
    }).map(key=>{
      return `${key}=${queryObj[key]}`
    })
    .join("&");
}
